package com.selfdriving.activity;

import net.tsz.afinal.annotation.view.ViewInject;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.dialog.MessageDialog;
import com.selfdriving.utils.DeviceUtils;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.MailTo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class FeatureJoinActivity extends BaseActivity implements
		OnClickListener {
	@ViewInject(id = R.id.bmapView)
	com.baidu.mapapi.map.MapView mapView;

	@ViewInject(id = R.id.ivMail, click = "ivMail")
	ImageView ivMail;

	@ViewInject(id = R.id.ivQQ, click = "ivQQ")
	ImageView ivQQ;

	@ViewInject(id = R.id.ivPhone, click = "ivPhone")
	ImageView ivPhone;

	private MessageDialog phoneDialog;

	private MessageDialog mailDialog;

	/**
	 * 用MapController完成地图控制
	 */
	private MapController mMapController = null;
	/**
	 * MKMapViewListener 用于处理地图事件回调
	 */
	MKMapViewListener mMapListener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feature_join);
		initMap();
	}

	public void ivPhone(View v) {
		phoneDialog = new MessageDialog(getParent(), "提示", "是否要拨打电话","确定", "取消", true,
				true, this);
		phoneDialog.show();
	}

	public void ivMail(View v) {
		mailDialog = new MessageDialog(getParent(), "提示", "是否要发送邮件", "确定", "取消",true,
				true, this);
		mailDialog.show();
	}

	private void initMap() {
		if (mApplication.mBMapManager == null) {
			mApplication.mBMapManager = new BMapManager(this);
			/**
			 * 如果BMapManager没有初始化则初始化BMapManager
			 */
			mApplication.mBMapManager.init(BaseApplication.strKey,
					new BaseApplication.MyGeneralListener(this));
		}

		/**
		 * 获取地图控制器
		 */
		mMapController = mapView.getController();
		/**
		 * 设置地图是否响应点击事件 .
		 */
		mMapController.enableClick(true);
		/**
		 * 设置地图缩放级别
		 */
		mMapController.setZoom(12);

		/**
		 * 将地图移动至指定点
		 * 使用百度经纬度坐标，可以通过http://api.map.baidu.com/lbsapi/getpoint/index
		 * .html查询地理坐标 如果需要在百度地图上显示使用其他坐标系统的位置，请发邮件至mapapi@baidu.com申请坐标转换接口
		 */
		GeoPoint p;
		double cLat = 39.945;
		double cLon = 116.404;
		Intent intent = getIntent();
		if (intent.hasExtra("x") && intent.hasExtra("y")) {
			// 当用intent参数时，设置中心点为指定点
			Bundle b = intent.getExtras();
			p = new GeoPoint(b.getInt("y"), b.getInt("x"));
		} else {
			// 设置中心点为天安门
			p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
		}

		mMapController.setCenter(p);

		/**
		 * MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		 */
		mMapListener = new MKMapViewListener() {
			@Override
			public void onMapMoveFinish() {
				/**
				 * 在此处理地图移动完成回调 缩放，平移等操作完成后，此回调被触发
				 */
			}

			@Override
			public void onClickMapPoi(MapPoi mapPoiInfo) {
				/**
				 * 在此处理底图poi点击事件 显示底图poi名称并移动至该点 设置过：
				 * mMapController.enableClick(true); 时，此回调才能被触发
				 * 
				 */
				String title = "";
				if (mapPoiInfo != null) {
					title = mapPoiInfo.strText;
					showToast(title);
					mMapController.animateTo(mapPoiInfo.geoPt);
				}
			}

			@Override
			public void onGetCurrentMap(Bitmap b) {
				/**
				 * 当调用过 mapView.getCurrentMap()后，此回调会被触发 可在此保存截图至存储设备
				 */
			}

			@Override
			public void onMapAnimationFinish() {
				/**
				 * 地图完成带动画的操作（如: animationTo()）后，此回调被触发
				 */
			}

			/**
			 * 在此处理地图载完成事件
			 */
			@Override
			public void onMapLoadFinish() {
				Toast.makeText(FeatureJoinActivity.this, "地图加载完成",
						Toast.LENGTH_SHORT).show();

			}
		};
		mapView.regMapViewListener(mApplication.mBMapManager, mMapListener);

		MyLocationOverlay myLocationOverlay = new MyLocationOverlay(mapView);
		LocationData locData = new LocationData();
		// 手动将位置源置为天安门，在实际应用中，请使用百度定位SDK获取位置信息，要在SDK中显示一个位置，需要使用百度经纬度坐标（bd09ll）
		locData.latitude = 39.945;
		locData.longitude = 116.404;
		locData.direction = 2.0f;
		myLocationOverlay.setData(locData);
		mapView.getOverlays().add(myLocationOverlay);
		mapView.refresh();
		mapView.getController().animateTo(
				new GeoPoint((int) (locData.latitude * 1e6),
						(int) (locData.longitude * 1e6)));
	}

	@Override
	protected void onPause() {
		/**
		 * MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		 */
		mapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		/**
		 * MapView的生命周期与Activity同步，当activity恢复时需调用MapView.onResume()
		 */
		mapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		/**
		 * MapView的生命周期与Activity同步，当activity销毁时需调用MapView.destroy()
		 */
		mapView.destroy();
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mapView.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dialog_generic_btn_button1:
			if (phoneDialog != null && phoneDialog.isShowing()) {
				phoneDialog.dismiss();
				DeviceUtils.callPhone(FeatureJoinActivity.this, "15279107963");
			} else {
				mailDialog.dismiss();
				DeviceUtils.sendMail(FeatureJoinActivity.this, "15279107963");
			}
			break;
		case R.id.dialog_generic_btn_button2:
			if (phoneDialog != null) {
				phoneDialog.dismiss();
			}
			if (mailDialog != null) {
				mailDialog.dismiss();
			}
			break;

		default:
			break;
		}

	}

}
